package com.googlecode.bipol.war.db.admin;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.sql.SQLException;
import java.sql.Timestamp;
import javax.naming.NamingException;
import javax.sql.DataSource;
import cx.ath.choisnet.sql.ExportSQL;

/**
 * @author Claude
 *
 */
public class DBExportSQL extends ExportSQL
{
    private final static ExportSQL.TableDescription[] exportTables = {
        new TableDescription( "local" ),
        new TableDescription( "category" ),
        new TableDescription( "category_local" ),
        new TableDescription( "categoryvalue_local" ),
        new TableDescription( "user" ),
        new TableDescription( "uservalue" ),
        new TableDescription( "usercomment" ),
        new TableDescription( "usercategory" ),
        new TableDescription( "userprivacy" ),
        new TableDescription( "referent" ),
        new TableDescription( "referentlogin" ),
        new TableDescription( "referentcomment" ),
        new TableDescription( "referents" ),
    };

    /**
     * 
     * @param ds 
     * @throws NamingException 
     */
    public DBExportSQL( 
            final DataSource    ds
            ) 
    {
        this(ds,null,new Timestamp(System.currentTimeMillis()));
    }
    
    /**
     * 
     * @param ds 
     * @param schemaName 
     * @throws NamingException 
     */
    public DBExportSQL( 
            final DataSource    ds,
            final String        schemaName
            ) 
    {
        this(ds,schemaName,new Timestamp(System.currentTimeMillis()));
    }
    
    /**
     * 
     * @param ds 
     * @param schemaName 
     * @param timestamp 
     * @throws NamingException 
     */
    public DBExportSQL( 
            final DataSource    ds,
            final String        schemaName, 
            final Timestamp     timestamp
            ) 
    {
        super(ds,schemaName,timestamp,exportTables);
    }
    
    /**
     * 
     * @return
     */
    public String getSafeTimeStampForFileName()
    {
        return getTimestamp().toString().replace( ':', '-' ).replace( ' ', '_' );
    }
    
    /**
     * 
     * @param outputFile
     * @throws IOException
     * @throws SQLException
     */
    public void export( final File outputFile ) throws IOException, SQLException
    {
        OutputStream fos = new FileOutputStream( outputFile );

        try {
            export( fos );
            }
        finally {
            fos.close();
            }
    }
    
    /**
     * 
     * @param outputFile
     * @throws IOException
     * @throws SQLException
     */
    public void export( final OutputStream fos ) throws IOException, SQLException
    {
        BufferedOutputStream bos = new BufferedOutputStream( fos );

        try {
            export( bos );
            }
        finally {
            bos.flush();
            bos.close();
            fos.close();
            }
    }

    /**
     * 
     * @param bos
     * @throws IOException
     * @throws SQLException
     */
    public void export( final BufferedOutputStream bos ) throws IOException, SQLException
    {
        Writer osw = new OutputStreamWriter( bos, "UTF-8" );

        try {
            super.exportAll( osw );
            }
        finally {
            osw.flush();
            osw.close();
            }
    }
}
